# anidb-search.pl
# vi: et sw=2
#
# AniDB search
# Copyright (C) 2009  Harry Karvonen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# 
# Author: Harry Karvonen <harry.karvonen@gmail.com>
#

#
# Version: 0.1
#

my $cmd = "anidb";
my $version = "0.1";
my $url_begin =	"http://anidb.info/perl-bin/animedb.pl?" .
		"show=animelist&do.search=search&adb.search=";

# action page http://anidb.info/perl-bin/animedb.pl
# Complete url pattern show, do.search, adb.search
# http://anidb.info/perl-bin/animedb.pl?show=animelist&adb.search=testihaku&do.search=search

Xchat::register("Anidb script", $version, "Give anidb url");

Xchat::hook_command($cmd, "anidb_command_hook");
Xchat::hook_server("PRIVMSG", "anidb_server_hook");

sub anidb_command_hook {
  my $search = $_[1][1];

  $search =~ s/ /+/g;

  $search = $url_begin . $search;

  Xchat::print("anidb\t $search");

  return Xchat::EAT_ALL;
}

sub anidb_server_hook {
  my $data = $_[1][0];

##  Xchat::print("$data");

  if ($data =~ m/:(\S+)!(\S+) PRIVMSG (\S+) :!anidb (.*)$/) {
    my $user = $1;
    my $user_host = $2;
    my $channel = $3;
    my $search = $4;
    my $server = Xchat::get_info("server");

    $search =~ s/ /+/g;

    $search = $url_begin . $search;

##    Xchat::print("$data", $channel);
##    Xchat::print("MSG $channel $search", $channel);
    Xchat::command("MSG $channel $search", $channel, $server);
##    Xchat::print("anidb\t $search");
  }
}

Xchat::print("Anidb url script loaded");
